﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PictureManager.Core;

namespace PictureManager.Controls
{
    public partial class BaseMaintain : BaseForm
    {
        #region Layout Keywords

        private string LayoutKey_Top
        {
            get { return this.GetType().ToString() + "_Top"; }
        }

        private string LayoutKey_Left
        {
            get { return this.GetType().ToString() + "_Left"; }
        }

        private string LayoutKey_Height
        {
            get { return this.GetType().ToString() + "_Height"; }
        }

        private string LayoutKey_Width
        {
            get { return this.GetType().ToString() + "_Width"; }
        }

        #endregion

        private System.Threading.Timer _layoutTimer;

        private Form topWindow;

        public BaseMaintain()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            topWindow = this.TopLevelControl as Form;

            if (topWindow != null && topWindow is WeifenLuo.WinFormsUI.Docking.FloatWindow)
            {
                //Hook layout events
                topWindow.Move += new EventHandler(TopWindow_Move);
                topWindow.ResizeEnd += new EventHandler(TopWindow_ResizeEnd);
            }

            //Initiate layout timer.
            _layoutTimer = new System.Threading.Timer((System.Threading.TimerCallback)delegate(object state)
            {
                System.Threading.Thread.Sleep(500);
                if (this.IsHandleCreated)
                {
                    try
                    {
                        topWindow.Invoke((MethodInvoker)delegate
                        {
                            if (topWindow != null)
                            {
                                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Left] = topWindow.Left.ToString();
                                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Top] = topWindow.Top.ToString();
                                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Width] = topWindow.Width.ToString();
                                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Height] = topWindow.Height.ToString();
                                ConfigurationManager.Instance.SaveCustomizedLayout();
                            }
                        });
                    }
                    catch
                    {
                        return;
                    }
                }
            }, null, -1, -1);
            IsLoaded = true;
        }

        protected virtual void TopWindow_Move(object sender, EventArgs e)
        {
            base.OnMove(e);
            if (this.IsLoaded)
            {
                _layoutTimer.Change(500, -1);
            }
        }

        protected virtual void TopWindow_ResizeEnd(object sender, EventArgs e)
        {
            base.OnResize(e);
            if (this.IsLoaded && topWindow != null)
            {
                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Left] = topWindow.Left.ToString();
                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Top] = topWindow.Top.ToString();
                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Width] = topWindow.Width.ToString();
                ConfigurationManager.Instance.CustomizedLayoutTable[LayoutKey_Height] = topWindow.Height.ToString();
                ConfigurationManager.Instance.SaveCustomizedLayout();
            }
        }
    }
}
